package com.lry.pda.bottom.mysql.frontend;

import java.util.List;
import java.util.Map;

public class ProductionManager {

    private Map<Integer, List<Production>> productionMap;

    private FirstSetBuilder firstSetBuilder;

    private static ProductionManager self;

    private ProductionManager(){
    }

    public void init(){
        //计算所有表达式的first set
        firstSetBuilder = new FirstSetBuilder();
        firstSetBuilder.runFirstSets();

        this.productionMap = Grammar.getSelf().getProductionMap();
    }

    public static ProductionManager getSelf(){
        if(null==self){
            self = new ProductionManager();
        }
        return self;
    }


    public List<Production> getProduction(int left) {
        return productionMap.get(left);
    }

    public FirstSetBuilder getFirstSetBuilder() {
        return firstSetBuilder;
    }

    public Production getProductionByNumber(int number) {
        for (List<Production> productions : productionMap.values()) {
            for (Production production : productions) {
                if(production.getNumber()==number){
                    return production;
                }
            }
        }
        return null;
    }
}
